<?php

namespace app\index\model;

use think\Cache;
use think\Db;
use think\Model;

class QjQuestionModel extends Model
{


    public function addQuestion($data)
    {
        $res['success'] = false;
        $res['desc'] = '添加失败';
        $res['errorcode'] = '200022';
        if (empty($data))  return $res;
        $ret = Db::table('qj_question')->insert($data);
        if ($ret) {
            $id = Db::table('qj_question')->getLastInsID();
            $res['success'] = true;
            $res['desc'] = '添加成功';
            $res['data']['id'] = $id;
            $res['errorcode'] = '';
        }
        return $res;
    }

    public function upQuestionInfo($question_id, array $upData)
    {
        Db::table('qj_question')->where(['id' => $question_id])->update($upData);
        $res['success'] = true;
        $res['desc'] = '修改成功';
        $res['errorcode'] = '';
        return $res;
    }

    public function delQuestionInfo($question_id)
    {
        Db::table('qj_question')->where(['id' => $question_id])->delete();
        $res['success'] = true;
        $res['desc'] = '删除成功';
        $res['errorcode'] = '';
        return $res;
    }
    public function getQuestionListByContent($content, $en_content, $msgId)
    {
        $where = [];
        if ($content && empty($en_content)) {
            $where['content'] = ['like', '%' . $content . '%'];
        } elseif (empty($content) && $en_content) {
            $where['en_content'] = ['like', '%' . $en_content . '%'];
        } elseif ($content && $en_content) {
            $where['content'] = ['like', '%' . $content . '%'];
        } elseif (empty($content) && empty($en_content)) {
            $res['success'] = false;
            $res['desc'] = '参数错误';
            $res['errorcode'] = '200022';
            return $res;
        }

        $ret = Db::table('qj_question')->where($where)->order('sort desc, id desc')->select();
        $modelQ = new QjQuestionLogModel();
        if (empty($ret)) {
            $model = new QjNewQuestionLogModel();
            if (empty($msgId)) {
                $res['success'] = false;
                $res['desc'] = 'msgId 未携带';
                $res['errorcode'] = '200022';
                return $res;
            }
            $insertData = [
                'content' =>  $content,
                'msg_id' => $msgId,
            ];
            $model->addNewQuestionLog($insertData);
            $insertData = [
                'question_id' => 0,
                'content' => $content,
                'msg_id' => $msgId,
            ];
            $modelQ->addQuestionLog($insertData);
        }
        $parentIds = [];
        foreach ($ret as $val) {
            $parentIds[] = $val['id'];
            $insertData = [
                'question_id' => $val['id'],
                'msg_id' => $msgId,
            ];
            $modelQ->addQuestionLog($insertData);
        }
        $childList = Db::table('qj_question')->where('parent_id', 'in', $parentIds)->select();

        $childListMap = [];
        foreach($childList as $cv) {
            $childListMap[$cv['parent_id']] = $cv;
        }
        $type = 0;
        $notEndIds = [];
        if (empty($childList)) {
            $type = 1;
            foreach ($ret as &$item) {
                $item['type'] = $type;
            }
        } else {
            foreach ($childList as $v) {
                $notEndIds[] = $v['id'];
            }
            foreach ($ret as &$item) {
                if (!in_array($item['id'], $notEndIds)) {
                    $type = 1;
                }
                $item['type'] = $type;
            }
        }
        $retData = [];
        foreach($ret as $k => $r) {
            if(!isset($childListMap[$r['id']]) || empty($childListMap[$r['id']])) {
               continue;
            }
            $retData[] = $r;
        }
        $res['success'] = true;
        $res['desc'] = '';
        $res['errorcode'] = '';
        $res['data']['list'] = $retData;
        return $res;
    }

    public function getQuestionList($content, $en_content, $pageSize, $page)
    {
        $where = [];
        $whereOr = [];

        if ($content && empty($en_content)) {
            $where['content'] = ['like', '%' . $content . '%'];
        } elseif (empty($content) && $en_content) {
            $where['en_content'] = ['like', '%' . $en_content . '%'];
        } elseif ($content && $en_content) {
            $where['content'] = ['like', '%' . $content . '%'];
            $whereOr['en_content'] = ['like', '%' . $en_content . '%'];
        }
        $where[] = ['status', '=', 1];
        $offset = ($page - 1) * $pageSize;
        if ($offset <= 0) {
            $offset = 0;
        }
        $ret = Db::table('qj_question')->where($where)->whereOr($whereOr)->limit($offset, $pageSize)->order('sort desc, id desc')->select();
        $total = Db::table('qj_question')->where($where)->whereOr($whereOr)->count();
        $res['success'] = true;
        $res['desc'] = '';
        $res['errorcode'] = '';
        $total_page = 0;
        if ($total) {
            $total_page = ceil($total / $pageSize);
        }
        $res['data']['list'] = $ret;
        $res['data']['total'] = $total;
        $res['data']['total_page'] = $total_page;
        return $res;
    }

    public function getQuestionInfo($question_id)
    {
        $info =  Db::table('qj_question')->where(['id' => $question_id])->find();
        $res['success'] = true;
        $res['desc'] = '';
        $res['errorcode'] = '';
        $res['data'] = $info;
        return $res;
    }
    public function getQuestionInfoNoPage($question_id, $msgId)
    {
        $list =  Db::table('qj_question')->where(['parent_id' => $question_id, 'status' => 1])->select();
        $parentIds = [];
        foreach ($list as $val) {
            $parentIds[] = $val['id'];
        }
        $childList = Db::table('qj_question')->where('parent_id', 'in', $parentIds)->select();
        $type = 0;
        $notEndIds = [];
        if (empty($childList)) {
            $type = 1;
            foreach ($list as &$item) {
                $item['type'] = $type;
            }
        } else {
            foreach ($childList as $v) {
                $notEndIds[] = $v['id'];
            }
            foreach ($list as &$item) {
                if (!in_array($item['id'], $notEndIds)) {
                    $type = 1;
                }
                $item['type'] = $type;
            }
        }
        $model = new QjQuestionLogModel();

        $insertData = [
            'question_id' => $question_id,
            'msg_id' => $msgId,
        ];
        $model->addQuestionLog($insertData);

        $res['success'] = true;
        $res['desc'] = '';
        $res['errorcode'] = '';
        $res['data']['list'] = $list;
        return $res;
    }
}
